import Vue from 'vue'
import Vuex from 'vuex'
import { setSession, getSession } from '@/utils/session'
import { tbSysUserUpdateSkin } from '@/api/admin/user/user-manage'

Vue.use(Vuex)

const state = {
	activeName: 'manage',
	activeRow: {},
	adminTheme: getSession('theme') || '#444', //后台主题
}

const getters = {
	pubAdminTheme(state) {
		return state.adminTheme
	},
}

const actions = {
	changeSkin({ commit }, skin) {
		let userinfo = getSession('userinfo')
		let { id } = JSON.parse(userinfo)
		setSession('theme', skin)
		commit('SET_ADMINTHEME', skin)
		tbSysUserUpdateSkin({ id, skin }).then((res) => {
			let userinfo = JSON.parse(getSession('userinfo'))
			userinfo.skin = skin
			setSession('userinfo', JSON.stringify(userinfo))
		})
	},
}

const mutations = {
	SET_ACTIVENAME: (state, d) => {
		state.activeName = d
	},
	SET_ACTIVEROW: (state, d) => {
		state.activeRow = d
	},
	SET_ADMINTHEME: (state, d) => {
		state.adminTheme = d
	},
}

export default {
	state,
	getters,
	actions,
	mutations,
}